from lxml import etree

import requests

# 提取代理API接口，获取1个代理IP
api_url = ""


proxy_ip = requests.get(api_url).text

username = ""
password = " "
proxies = {
    "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
    "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
}
print(proxies)

url = "https://movie.douban.com/top250"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88'
                  'Safari/537.36'
}
response = requests.get(url, headers=headers, proxies=proxies)
page_source = response.text

tree = etree.HTML(page_source)
movies = tree.xpath("//ol[@class='grid_view']/li")

# 提取电影信息
movie_data = []
for movie in movies:
    title = movie.xpath(".//span[@class='title']/text()")[0]
    rating = movie.xpath(".//span[@class='rating_num']/text()")[0]
    info = movie.xpath(".//div[@class='bd']/p/text()")[0].strip()
    movie_data.append([title, rating, info])
    print(title, rating, info)
